package com.adun;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * 分代测试Date日期与时间
 */
public class DateTest {
    public static void main(String[] args) {
        //第一代日期
        System.out.println("==============================第一代日期================================");
        SimpleDateFormat simpleDateFormat1 = DateOne();

        //第二代日期:不能实例化：Calendar日历类
        System.out.println("\n\n==============================第二代日期================================");
        DateTwo(simpleDateFormat1);


        //第三代日期
        System.out.println("\n\n==============================第三代日期================================");
        DateThree();

    }

    private static void DateThree() {
        //获取当前系统的最新时间【2020-12-15T22:22:32.941】
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);
        LocalDate now1 = LocalDate.now();
        System.out.println(now1);
        LocalTime now2 = LocalTime.now();
        System.out.println(now2);

        //date=>String
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String format2 = dateTimeFormatter.format(now);
        System.out.println(format2);

        //String=>date不会出现受检异常
        String s="2020-11-01 13:14:11";
        LocalDateTime parse1 = LocalDateTime.parse(s, dateTimeFormatter);
        System.out.println(parse1);
    }

    private static void DateTwo(SimpleDateFormat simpleDateFormat1) {
        //创建日历实例
        Calendar instance = Calendar.getInstance();
        //获取系统的当前时间
        Date time = instance.getTime();
        //Tue Dec 15 22:22:32 CST 2020
        System.out.println(time);
        int date2 = instance.get(Calendar.DATE);
        System.out.println(date2);
        int year = instance.get(Calendar.YEAR);
        int month = instance.get(Calendar.MONTH)+1;
        int day = instance.get(Calendar.DAY_OF_MONTH);
        System.out.println(year+"-"+month+"-"+day);

        int hour = instance.get(Calendar.HOUR_OF_DAY);
        int minute = instance.get(Calendar.MINUTE);
        int second = instance.get(Calendar.SECOND);
        System.out.println(year+"-"+month+"-"+day+"\t"+hour+":"+minute+":"+second);
        //date=>format
        System.out.println(simpleDateFormat1.format(time));
    }

    private static SimpleDateFormat DateOne() {
        //获取当前系统的最新时间=====》Tue Dec 15 22:22:32 CST 2020
        Date date1 = new Date();
        System.out.println(date1);
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format1 = simpleDateFormat1.format(date1);
        System.out.println("Date=>String\t"+format1);
        //受检异常：日期的格式可能与设置的格式不匹配
        Date parse=null;
        try {
            parse= simpleDateFormat1.parse("2020-10-10 12:13:14");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.printf("String=>Date\t"+parse);
        return simpleDateFormat1;
    }
}
